// pages/auth/Identity_selection/Identity_selection.js
import { requst_get_user_reg, requst_get_user_detail } from '../../../api/login.js'
import { wxUploadFile } from "../../../utils/request";

import TencentCloudChat from '@tencentcloud/chat';
import { genTestUserSig } from '../../../debug/GenerateTestUserSig';

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    windowHeight: '',//屏幕高度
    select_zt: '1',
    showBottom: false,
    avatarUrl: 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
    nickname: '',
    theme: wx.getSystemInfoSync().theme,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    //屏幕信息
    wx.getSystemInfo({
      success: function (res) {
        // 高度单位为px
        that.setData({
          windowHeight: res.windowHeight,
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  select(e) {
    var that = this;
    const data = e.currentTarget.dataset
    that.setData({
      select_zt: data.select,
      showBottom: true
    })
  },
  save() {
    if (this.data.avatarUrl == 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0' || !this.data.nickname) {
      wx.showToast({
        title: '请完善头像及昵称！',
        icon: 'none'
      })
      return
    }
    let inviteid = wx.getStorageSync('invateId')
    let that = this
    let params = {
      avatar: this.data.avatarUrl,
      nickname: this.data.nickname,
      limits_id: this.data.select_zt,
      invite_id: inviteid ? inviteid : ''
    }
    requst_get_user_reg(params).then(res => {
      requst_get_user_detail({}).then(res => {
        if (res.data.user) {
          wx.setStorage({
            key: "user",
            data: res.data.user
          })
          that.handleIM()
        }
      })
    })
  },
  handleIM() {
    wx.$TUIKit = TencentCloudChat.create({
      SDKAppID: 1600041146
    });
    const userSig = genTestUserSig({
      userID: this.data.nickname,
      SECRETKEY: '654b371d6ee2456f6d03d9ec46dcd2e1d8fbd4fa4712a9974df98b4202d3a1e1',
      SDKAPPID: 1600041146,
      EXPIRETIME: 604800,
    }).userSig
    wx.$TUIKit.login({
      userID: this.data.nickname,
      userSig
    });
    wx.switchTab({ url: '/pages/index/index' })
  },
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail
    wxUploadFile({
      url: "/upload",
      filePath: avatarUrl,
      formData: {},
    }).then((data) => {
      if (data.data && data.data.file_path) {
        this.setData({
          avatarUrl: 'https://mote-wx.sunchanghao.top' + data.data.file_path, // 更新数据绑定的值
        })
      }
    });
  },
  onClose() {
    this.setData({
      avatarUrl: 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
      nickname: '',
      showBottom: false
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})